import api from '../api'
import settings from '~/conf/settings'

const state = {
  me: {},
  token: {},
  token_loaded: false,
  user_loaded: false,
}

const valid_token = (token) => {
  return !!token && !!token.token //&& token.expire_at * 1000 > new Date().getTime()
}

const getters = {

  user_authorized: state => valid_token(state.token)
}

const actions = {

  userList({commit, dispatch}, params) {
    return api.get(api.url('userList'), params)
  },

  // 登录
  signin({commit, dispatch}, {account, password}) {
    let params = {
        account: account,
        password: password
    }
    //这里判断是餐馆 还是总店

    api.post(api.url('bistroLogin'), params)
       .then((ret) => {
             let store = window.localStorage;
             store.setItem('backend_token', JSON.stringify(ret));
             console.log(ret)
             commit('token_loaded', ret);
             dispatch('load_user');
       })
  },

  signinHead({commit, dispatch}, {account, password}) {
    let params = {
      account: account,
      password: password
    }
    //这里判断是餐馆 还是总店

    api.post(api.url('login'), params)
      .then((ret) => {
        let store = window.localStorage;
        store.setItem('backend_token', JSON.stringify(ret));
        // console.log(ret, 'wocao')

        commit('token_loaded', ret);
        dispatch('load_head_store');
      })
  },

  // 登出
  signout({commit}) {
    window.localStorage.removeItem('backend_token')
    commit('signout')
  },

  // 载入token
  load_token({commit, dispatch}) {
    let ret = new Promise((resolve, reject) => {
      let store = window.localStorage;
      let token = {}
      try {
        token = JSON.parse(store.getItem('backend_token'))
      } catch (e) {}
      if (!valid_token(token)) {
        commit('token_loaded', {})
        reject('')
      } else {
        commit('token_loaded', token)
        resolve(token)
      }
    });

    return ret
  },

  updateManagerPass({commit, dispatch}, params) {
    return api.put(api.url('updatePassWord', params.id), params)
  },



  // 载入用户
  load_user({commit, dispatch}) {
    api.get(api.url('bistroInformation')).then(ret => {
      if (ret.success) {
        commit('user_loaded', ret.data);
      } else {
        dispatch('signout')
      }
    })
  },

  //载入总店用户
  load_head_store({commit, dispatch}) {//headStoreInformation
    api.get(api.url('headStoreInformation')).then(ret => {
      if (ret.success) {
        commit('user_loaded', ret.data);
      } else {
        dispatch('signout')
      }
    })
  },

  getManager({commit, dispatch}, params) {
    return api.get(api.url('managerList'), params)
  }

}

const mutations = {
  user_loaded(state, me) {
    state.me = me
    state.user_loaded = true
  },
  token_loaded(state, token) {
    state.token = token
    state.token_loaded = true
  },

  signout(state) {
    state.token = {}
    state.me = {}
    state.token_loaded = true
    state.user_loaded = false
  }
}

export default {
  state,
  getters,
  actions,
  mutations
}
